/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.upc.sad.controller;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.TooManyListenersException;
import javax.servlet.http.HttpSession;
import pe.edu.upc.sad.dao.EmpresaAreaDao;
import pe.edu.upc.sad.dao.EmpresaAreaDaoImpl;
import pe.edu.upc.sad.dao.TipoAreaDao;
import pe.edu.upc.sad.dao.TipoAreaDaoImpl;
import pe.edu.upc.sad.model.EmpresaArea;
import pe.edu.upc.sad.model.MensajeResultado;
import pe.edu.upc.sad.model.TipoArea;
import pe.edu.upc.sad.util.Conexion;
import pe.edu.upc.sad.util.ToolSql;
/**
/**
 *
 * @author HP
 */
public class ListarEmpresaArea extends HttpServlet {

    EmpresaAreaDao tarea = new EmpresaAreaDaoImpl();

    @Override
    public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {
        Gson gson = new Gson();

         List<EmpresaArea> lstArea = null ;
        try
        {
             String sid_tipo=(request.getParameter("tipo") != null) ?  request.getParameter("tipo").toString()  : "";
             int id_tipo=(sid_tipo.toString().equals("")) ?   0 : Integer.parseInt(sid_tipo);

            String nombre=(request.getParameter("nombre") != null) ?   request.getParameter("nombre").toString() : "";

            String limit=(request.getParameter("limit") != null) ?   request.getParameter("limit").toString() : "6";
            String start=(request.getParameter("start") != null) ?   request.getParameter("start").toString() : "0";
            String dir=(request.getParameter("dir") != null) ?   request.getParameter("dir").toString() : "DESC";
            String sort=(request.getParameter("sort") != null) ?   request.getParameter("sort").toString() : "Nombre";

            int p_start = Integer.parseInt(start);
            int p_limit = Integer.parseInt(limit);
            nombre = "%" + nombre + "%";
            lstArea = tarea.getListAreaEmpresa(id_tipo, nombre,p_start,p_limit,dir,sort);

            int count =  tarea.getListAreaEmpresaCount(id_tipo, nombre);
            String json = "{'Empresaarea':";
            json += gson.toJson(lstArea) + ",'oTotReg':"+ count +"}";// gson.toJson(obj);

            //response.setContentType("text/plain");
            response.setContentType("text/x-json;charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            //response.setContentLength(message.length());
            PrintWriter out = response.getWriter();
            out.println(json);

            out.close();
            out.flush();

        }catch(Exception e)
        {
            lstArea = new ArrayList<EmpresaArea>();
            String json = "{'Empresaarea':";
            json += gson.toJson(lstArea)  + ",'oTotReg':0}";

            //response.setContentType("text/plain");
            response.setContentType("text/x-json;charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            //response.setContentLength(message.length());
            PrintWriter out = response.getWriter();
            out.println(json);

            out.close();
            out.flush();

        }
    }

    @Override
    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {

    }

}
